/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mytdev.commons.lang.bean;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 *
 * @author Yann D'Isanto
 */
public class ManagedBean extends BeanManager {

    /**
     * Creates a ManagedBean then invokes methods annotated with @OnCreate annotation 
     * from this object.
     * @throws com.mytdev.commons.lang.bean.ManagedBean.CreationException if an error 
     * occurs when invoking OnCreate annotated methods.
     */
    public ManagedBean() throws CreationException {
        try {
            invokeAnnotatedMethods(this, OnCreate.class);
        } catch (Exception ex) {
            throw new CreationException(ex);
        }
    }

    /**
     * Invokes methods annotated with @OnClose annotation from this object.
     * @throws com.mytdev.commons.lang.bean.BeanManager.CloseException if an error 
     * occurs when invoking OnClose annotated methods.
     */
    public void close() throws CloseException {
        close(this);
    }

    /**
     * Invokes methods annotated with @OnFinalize annotation from this object.
     * @throws Throwable the Exception raised by this method.
     */
    @Override
    protected final void finalize() throws Throwable {
        super.finalize();
        invokeAnnotatedMethods(this, OnFinalize.class);
    }

    /**
     * Invokes methods annotated with @OnInit annotation from this object.
     * @throws com.mytdev.commons.lang.bean.BeanManager.InitException if an error 
     * occurs when invoking OnInit annotated methods.
     */
    public void init() throws InitException {
        init(this);
    }

    /**
     * Invokes methods annotated with @OnLoad annotation from this object.
     * @throws com.mytdev.commons.lang.bean.BeanManager.LoadException if an error 
     * occurs when invoking OnLoad annotated methods.
     */
    public void load() throws LoadException {
        load(this);
    }

    /**
     * Invokes methods annotated with @OnSave annotation from this object.
     * @throws com.mytdev.commons.lang.bean.BeanManager.SaveException if an error 
     * occurs when invoking OnSave annotated methods.
     */
    public void save() throws SaveException {
        save(this);
    }

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public static @interface OnCreate {
    }

    public static class CreationException extends RuntimeException {

        public CreationException() {
        }

        public CreationException(String message) {
            super(message);
        }

        public CreationException(String message, Throwable cause) {
            super(message, cause);
        }

        public CreationException(Throwable cause) {
            super(cause);
        }
    }

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public static @interface OnFinalize {
    }

}
